package com.hsogoo.study.autoconfiguration;

import com.hsogoo.study.autoconfiguration.bean.Person;
import com.hsogoo.study.autoconfiguration.bean.User;
import com.hsogoo.study.autoconfiguration.bean.Work;
import com.hsogoo.study.autoconfiguration.config.HelloConfiguration;
import com.hsogoo.study.autoconfiguration.config.HelloImportBeanDefinitionRegistrar;
import com.hsogoo.study.autoconfiguration.config.HelloImportSelector;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * @Author huangsaigang
 * Created @2019-09-29.
 */
@Configuration
@Import({User.class, HelloConfiguration.class,HelloImportBeanDefinitionRegistrar.class, HelloImportSelector.class})
public class ImportAnnotationApplication {

    public static void main(String[] args) {

        //非web方式启动
        ConfigurableApplicationContext context = new SpringApplicationBuilder(ImportAnnotationApplication.class)
                .web(WebApplicationType.NONE)
                .run(args);

        //通过@Configuration导入
        User user = context.getBean(User.class);
        System.out.println("================通过普通类导入===========================================");
        System.out.println(user);
        //通过@Configuration导入
        String myBean = context.getBean("myBean", String.class);
        System.out.println("================通过@Configuration导入===================================");
        System.out.println(myBean);
        //通过ImportBeanDefinitionRegistrar导入
        Person person = context.getBean("person", Person.class);
        System.out.println("=================通过ImportBeanDefinitionRegistrar导入===================");
        System.out.println(person);
        //通过ImportSelector导入
        Work work = context.getBean(Work.class);
        System.out.println("=================通过ImportSelector导入==================================");
        System.out.println(work);

        //关闭上下文
        context.close();

    }
}
